package com.village.zpark.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.village.zpark.mapper.FarmerMapper;
import com.village.zpark.mapper.TransportationMapper;
import com.village.zpark.pojo.Farmer;
import com.village.zpark.pojo.Transportation;
import com.village.zpark.service.TransportationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
@Transactional
public class TransportationServiceImpl implements TransportationService {
    @Autowired
    private TransportationMapper transportationMapper;
    @Override
    public List<Transportation> findAll() {
        return transportationMapper.selectAll();
    }

    @Override
    public Transportation findById(Integer id) {
        return transportationMapper.selectByPrimaryKey(id);
    }

    @Override
    public int addTransportation(Transportation transportation) {
        return transportationMapper.insert(transportation);
    }

    @Override
    public int updateTransportation(Transportation transportation) {
        return transportationMapper.updateByPrimaryKey(transportation);
    }

    @Override
    public int deleteTransportationById(Integer id) {
        return transportationMapper.deleteByPrimaryKey(id);
    }

    @Override
    public PageInfo<Transportation> findPage(int page, int size, String param) {
        PageHelper.startPage(page, size);
        Example example = new Example(Farmer.class);
        Example.Criteria criteria = example.createCriteria();
        if (param != null && !param.isEmpty()) {
            criteria.orLike("roadtype", "%" + param + "%")
                    .orLike("material", "%" + param + "%");; // 使用 orLike 来实现多字段模糊查询
        }
        List<Transportation> transportations;
        if (param != null && !param.isEmpty()) {
            transportations = transportationMapper.selectByExample(example);
        } else {
            transportations = transportationMapper.selectAll(); // 当搜索栏为空时，返回全部表单信息
        }
        return new PageInfo<>(transportations);
    }
}
